package com.sdsy.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SysUtil {


    /**
     * 判断是否为空
     *
     * @param
     * @return
     */
    public static boolean isNull(Object obj) {
        if (obj == null || "null".equalsIgnoreCase(String.valueOf(obj))) return true;
        else {
            if ("".equalsIgnoreCase(String.valueOf(obj).trim())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 生成唯一id
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    //生成随机数字和字母,  
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数  
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字  
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }


    public static String generateRandomStr(int len) {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < len; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};


    /**
     * 6位 100000
     * 4位 1000
     *
     * @param m
     * @return
     */
    public static String getRandom(int m) {
        int i = (int) ((Math.random() * 9 + 1) * m);
        return i + "";
    }

    /**
     * null 转 ""
     *
     * @param str
     * @return
     */
    public static String toStrEmpty(String str) {
        return isNull(str) ? "" : str;
    }

    public static DecimalFormat CURRENCY_DF = new DecimalFormat("#0.000000000000000000"); //金额格式化



    /**
     * 替换html标签
     */
    public static String delHTMLTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); //过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); //过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }


    /**
     * 根据状态码返回的值判断
     *
     * @param status
     * @return
     */
    public static boolean isResult(String status) {
        BigDecimal fh_status = new BigDecimal(status);
        if (fh_status.compareTo(new BigDecimal(10000)) > 0 && fh_status.compareTo(new BigDecimal(20000)) <= 0) {
            return true;
        } else {
            return false;
        }
    }



    public static <T> String objectToStr(T t) {
        ObjectMapper mapper = new ObjectMapper();
        String str = null;
        try {
            str = mapper.writeValueAsString(t);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


